package robbie.WeatherApp;

import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;

import android.os.Bundle;
import android.view.View;
import android.widget.*;
import android.app.*;
import android.content.*;

import java.io.*;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;



public class ShowInfoActivity extends ListActivity
{
	InformatiiAdapter adapterS;
	Button b;
	TextView t;
	
    String s[]=new String[22];
    int k=0;

	
	public void onCreate(Bundle savedInstanceBundle) 
    {

        super.onCreate(savedInstanceBundle);
        adapterS = new InformatiiAdapter(this);
        setContentView(R.layout.show);
        setListAdapter(adapterS);
        int h = 0;

        t = (TextView)findViewById(R.id.ShowTextView);
        b = (Button)findViewById(R.id.ShowButtonBack);
        ProgressDialog dialog = ProgressDialog.show(ShowInfoActivity.this, "", "Loading. Please wait...", true);

                    try
                    {
 
                    	Intent primeste = getIntent();  
                    	String ss = primeste.getStringExtra("numeOras");
                    	ss=ss.replaceAll(" ", "%20");

  
    //                	URI uri = new URI("http://www.google.com/ig/api?weather="+ss);
                        HttpGet getMethod=new HttpGet("http://www.google.com/ig/api?weather="+ss);
                        HttpClient client = new DefaultHttpClient ();
                        
         //               String w = primeste.getStringExtra("nume_oras"); 
                        
         /*               if(w.contains(" "))
                        {
                        	String[] q = w.split(" ");
                        	String p="";
                        	for(int i=0;i<q.length;i++)
                        		p=p+q[i].substring(0, 1).toUpperCase()+q[i].substring(1, q[i].length())+" ";
                        	t.setText(p);
                        }
                        else */
                        	t.setText(primeste.getStringExtra("numeOras").substring(0, 1).toUpperCase()+primeste.getStringExtra("numeOras").substring(1, primeste.getStringExtra("numeOras").length())+":");
                        
                    	ResponseHandler<String> responseHandler=new BasicResponseHandler();
                    	String responseBody=client.execute(getMethod, responseHandler);
                    	
                    	System.out.println(responseBody);

                    	XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
                    	factory.setNamespaceAware(true);
                    	XmlPullParser xpp = factory.newPullParser();

                    	xpp.setInput( new StringReader (responseBody) );
                    	int eventType = xpp.getEventType();
                    	
                    	

                    	while (eventType != XmlPullParser.END_DOCUMENT) 
                    	{
                    		if ( (eventType == XmlPullParser.START_TAG) && (xpp.getName().equals("problem_cause") ) )
                    		{
                    			h=1;                    			
                    		}
                    		
                    		if ( (eventType == XmlPullParser.START_TAG) && (xpp.getName().equals("forecast_date") ) )
                    		{
                    			s[k]=xpp.getAttributeValue(0);
                    			k++;
                    		}
                    		if ( (eventType == XmlPullParser.START_TAG) && (xpp.getName().equals("condition") ) )
                    		{
                    			s[k]=xpp.getAttributeValue(0);
                    			k++;
                    		}
                    		if ( (eventType == XmlPullParser.START_TAG) && (xpp.getName().equals("temp_c") ) )
                    		{
                    			s[k]=xpp.getAttributeValue(0);
                    			k++;
                    		}
                    		if ( (eventType == XmlPullParser.START_TAG) && (xpp.getName().equals("humidity") ) )
                    		{
                    			s[k]=xpp.getAttributeValue(0);
                    			k++;
                    		}
                    		if ( (eventType == XmlPullParser.START_TAG) && (xpp.getName().equals("icon") ) )
                    		{
                    			s[k]=xpp.getAttributeValue(0);
                    			k++;
                    		}
                    		if ( (eventType == XmlPullParser.START_TAG) && (xpp.getName().equals("wind_condition") ) )
                    		{
                    			s[k]=xpp.getAttributeValue(0);
                    			k++;
                    		}
                    		if ( (eventType == XmlPullParser.START_TAG) && (xpp.getName().equals("forecast_conditions") ) )
                    		{
                    			
                    			while ( eventType != XmlPullParser.END_TAG || (!xpp.getName().equals("forecast_conditions")) )		
                    			{
                            		if ( (eventType == XmlPullParser.START_TAG) && (xpp.getName().equals("day_of_week") ) )
                            		{
                            			s[k]=xpp.getAttributeValue(0);
                            			k++;
                            		}
                            		if ( (eventType == XmlPullParser.START_TAG) && (xpp.getName().equals("low") ) )
                            		{
                            			s[k]=xpp.getAttributeValue(0);
                            			k++;
                            		}
                            		if ( (eventType == XmlPullParser.START_TAG) && (xpp.getName().equals("icon") ) )
                            		{
                            			s[k]=xpp.getAttributeValue(0);
                            			k++;
                            		}
                            		if ( (eventType == XmlPullParser.START_TAG) && (xpp.getName().equals("condition") ) )
                            		{
                            			s[k]=xpp.getAttributeValue(0);
                            			k++;
                            		}
                            		
                            		eventType = xpp.next();
                    			}

                    		}
                    		
                    		eventType = xpp.next();
                    	}

                    }
                    catch(XmlPullParserException e)
                    {
                    	System.err.println("exceptie XPPE");   
                    	h=2;
       //             	adapterS.adaugaInformatie("No internet connection","" , "semnul_exclamarii"	);
                    }
                    catch(IOException e)
                    {
                    	System.err.println("exceptie IOE");
               	
                    	
                    }
                    catch (Throwable t) 
                    {
                    	System.out.println("eroare Throwable");
                    	t.printStackTrace();
                    }

                    

                	System.out.println("//////////////////////////////////////////////////////////////");
        //        	for (int i=0;i<s.length;i++)
        //        	{
        //        		System.out.println(s[i]);
        //        	}
                	
                	
                	if (h==0)creeaza_lista(s);
                	else if (h==1)
                		{
                			adapterS.adaugaInformatie("This city is not in the database", "","semnul_exclamarii");
                		
                		}
                	else if (h==2)
                	{
                		adapterS.adaugaInformatie("No internet connection","" , "semnul_exclamarii"	);
                			
               		}
                	

        
        b.setOnClickListener(new View.OnClickListener(){
        	
		
			public void onClick(View v) {
				finish();			
			}
        });
        dialog.dismiss();
    
    }
	
	public void creeaza_lista(String[] s)
	{
		adapterS.adaugaInformatie("", "", "no_icon");
    	adapterS.adaugaInformatie("Forecast date: ", s[0], "");
    	adapterS.adaugaInformatie("Condition: ", s[1], s[4]);
    	adapterS.adaugaInformatie("Temperature (C) : ", s[2], "no_icon");
    	adapterS.adaugaInformatie("", s[3], "no_icon");
    	adapterS.adaugaInformatie("", s[5], "no_icon");
    	adapterS.adaugaInformatie("", "", "no_icon");
    	adapterS.adaugaInformatie("Day: ", s[6]+"  ( today ) ", "no_icon");
    	adapterS.adaugaInformatie("Low temp (C): ", ""+((Integer.parseInt(s[7])-32)*5/9), "no_icon");
    	adapterS.adaugaInformatie("Condition: ", s[9], s[8]);
    	adapterS.adaugaInformatie("", "", "no_icon");
    	adapterS.adaugaInformatie("Day: ", s[10], "no_icon");
    	adapterS.adaugaInformatie("Low temp (C): ",""+((Integer.parseInt(s[11])-32)*5/9), "no_icon");
    	adapterS.adaugaInformatie("Condition: ", s[13], s[12]);
    	adapterS.adaugaInformatie("", "", "no_icon");
    	adapterS.adaugaInformatie("Day: ", s[14], "no_icon");
    	adapterS.adaugaInformatie("Low temp (C): ", ""+((Integer.parseInt(s[15])-32)*5/9), "no_icon");
    	adapterS.adaugaInformatie("Condition: ", s[17], s[16]);
    	adapterS.adaugaInformatie("", "", "no_icon");
    	adapterS.adaugaInformatie("Day: ", s[18], "no_icon");
    	adapterS.adaugaInformatie("Low temp (C): ", ""+((Integer.parseInt(s[19])-32)*5/9), "no_icon");
    	adapterS.adaugaInformatie("Condition: ", s[21], s[20]);
    	adapterS.adaugaInformatie("", "", "no_icon");

        
	}
	

}

